<?php
namespace Home\Controller;
use Think\Controller;

class PayController extends CommonController {
	
	public function index(){
		
	}
	
	
	public function ajaxSubmitPay(){
		$payment_id = I('payment_id',null);
		$pay_type = I('pay_type','');
		$paySrv = D('PaymentInfo');
		$payment_info = $paySrv->where("payment_id = '".$payment_id."'")->find();
		
		$payment_amount = num_f($payment_info['should_payment']);
		$payment_status = $payment_info['payment_status'];
		if($payment_status!=C('PAYMENT_STATUS.WAITING_PAY')){
			$response['rsc'] = C('RESPONSE.FAILED');
            $response['msg'] = '操作失败，账单不可支付';
            $this->ajaxReturn($response);
		}
		// 判断订单是否已经是否进行中
		$order_id = $payment_info['order_id'];
		$orderSrv = D('OrderInfo');
		$order_info = $orderSrv->where("order_id = '".$order_id."'")->find();
		if($order_info['order_status'] != C('ORDER_STATUS.WAITING_PROCESS')){
			$response['rsc'] = C('RESPONSE.FAILED');
            $response['msg'] = '操作失败，订单状态异常';
            $this->ajaxReturn($response);
		}
		
		if($pay_type=='wxmpay' || $pay_type=='alipay'){
			$paymentOnlineFlow = D('PaymentOnlineFlow');
			$data['flow_id'] = new_doc_id('ONLINE_FLOW');
			$data['type'] = 0;
			$data['status'] = 0;
			$data['relation_id'] = $payment_id;
			$data['should_pay'] = $payment_amount;
			$paymentOnlineFlow->data($data)->add();
			$response['flow_id'] = $data['flow_id'];
			$response['rsc'] = C('RESPONSE.SUCCESS');
			$response['msg'] = '生成成功';
			$this->ajaxReturn($response);
		}
	}
	
};